package algorithms;

import java.util.Arrays;
import java.util.List;

/**
 * @author lizen
 * example:
 * 666 => 18 => 9
 * 10509 => 15 => 6
 */
public class SumNumberStringByDigital {
    public static void test() {
        String[] numberString = new String[]{"9999", "10", "123"};
        List<String> resultArr = Arrays.stream(numberString).map(SumNumberStringByDigital::tillOnlyOneDigitalLeft).toList();
        System.out.println(resultArr);
    }

    public static String tillOnlyOneDigitalLeft(String s)
    {
        while (s.length() > 1){
            int temp = 0;
            for (int index = 0; index < s.length(); index++) {
                char charValue = s.charAt(index);
                String stringVal = String.valueOf(charValue);
                int intValue = Integer.parseInt(stringVal);
                temp += intValue;
            }
            s = Integer.toString(temp);
        }

        return s;
    }
}
